home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
newton
/
slider.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
7KB
|
375 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* slider stuff
* Yossi Friedman, July 1988
*/
#include <stdio.h>
#include <gl.h>
#include <device.h>
#include "slider.h"
void draw_slider(int sid);
/*
* indices into a coordinate vector
*/
#define X 0
#define Y 1
static struct slider_state {
char *progname;
char *title;
int wid;
int menu;
long orig[2];
short state;
short omx;
float unit;
float ctl_points[6][2];
float lo, hi, dflt;
float val; /* current value of the slider */
void (*fun)(float);
} sliders[100], *end_sliders = sliders;
/* values for state field of slider_state structure */
#define SLIDER_IDLE 0
#define SLIDER_SLIDING 1
int
define_slider(
char *progname, char *title,
float lo, float hi, float dflt,
void (*fun)(float))
{
end_sliders->progname = (char *)malloc(strlen(progname) + 1);
if (end_sliders->progname == NULL) {
fprintf(stderr, "slider: Could not malloc\n");
my_exit(1);
}
strcpy(end_sliders->progname, progname);
end_sliders->title = (char *)malloc(strlen(title) + 1);
if (end_sliders->title == NULL) {
fprintf(stderr, "slider: Could not malloc\n");
my_exit(1);
}
strcpy(end_sliders->title, title);
end_sliders->lo = lo;
end_sliders->hi = hi;
end_sliders->dflt = dflt;
end_sliders->val = dflt;
end_sliders->fun = fun;
/* (*end_sliders->fun)(dflt); -- compiler bug. */
(*fun)(dflt);
end_sliders->unit = (hi - lo) / 500.;
end_sliders->state = SLIDER_IDLE;
end_sliders->ctl_points[0][X] = 10.;
end_sliders->ctl_points[2][X] = 510.;
end_sliders->ctl_points[0][Y] =
end_sliders->ctl_points[1][Y] =
end_sliders->ctl_points[2][Y] = 20.;
end_sliders->ctl_points[3][X] = 510.;
end_sliders->ctl_points[5][X] = 10.;
end_sliders->ctl_points[3][Y] =
end_sliders->ctl_points[4][Y] =
end_sliders->ctl_points[5][Y] = 40.;
end_sliders->wid = -1;
end_sliders->menu = NULL;
return(end_sliders++ - sliders);
}
long
open_slider(int sid)
{
struct slider_state *sp = &sliders[sid];
long wid = winget();
if (sp->wid == -1) {
prefsize(520, 50);
sp->wid = winopen(sp->progname);
prefsize(520, 50);
winconstraints();
wintitle(sp->title);
getorigin(&sp->orig[X], &sp->orig[Y]);
ortho2(0., 520., 0., 50.);
doublebuffer();
RGBmode();
gconfig();
}
else {
winset(sp->wid);
winpop();
}
draw_slider(sid);
if (wid != -1)
winset(wid);
return(sp->wid);
}
int
close_slider(int sid)
{
struct slider_state *sp = &sliders[sid];
if (sp->menu != NULL) {
freepup(sp->menu);
sp->menu = NULL;
}
winclose(sp->wid);
sp->wid = -1;
}
int
set_slider_default(int sid, float val)
{
sliders[sid].dflt = val;
sliders[sid].menu = NULL; /* force new menu to be created */
}
int set_slider(int sid, float val)
{
void (*fun)(float); /* compiler bug */
sliders[sid].val = val;
draw_slider(sid);
fun = sliders[sid].fun;
(*fun)(val);
}
void
draw_slider(int sid)
{
struct slider_state *sp = &sliders[sid];
long wid = winget();
char buf[32];
float dx;
if (sp->wid == -1)
return;
winset(sp->wid);
RGBcolor(200, 200, 200);
clear();
RGBcolor(0, 0, 0);
cmov2(200., 5.);
sprintf(buf, "%5f", sp->val);
charstr(buf);
cmov2(5., 5.);
sprintf(buf, "%3f", sp->lo);
charstr(buf);
cmov2(420., 5.);
sprintf(buf, "%5f", sp->hi);
charstr(buf);
dx = (sp->val - sp->lo) / sp->unit;
sp->ctl_points[1][X] =
sp->ctl_points[4][X] = dx + 10.;
RGBcolor(0, 200, 0);
bgnpolygon();
v2f(sp->ctl_points[0]);
v2f(sp->ctl_points[1]);
v2f(sp->ctl_points[4]);
v2f(sp->ctl_points[5]);
endpolygon();
RGBcolor(200, 200, 0);
bgnpolygon();
v2f(sp->ctl_points[1]);
v2f(sp->ctl_points[2]);
v2f(sp->ctl_points[3]);
v2f(sp->ctl_points[4]);
endpolygon();
RGBcolor(0, 0, 0);
bgnclosedline();
v2f(sp->ctl_points[0]);
v2f(sp->ctl_points[2]);
v2f(sp->ctl_points[3]);
v2f(sp->ctl_points[5]);
endclosedline();
swapbuffers();
if (wid != -1)
winset(wid);
}
int
slider_event(int sid, short dev, short val)
{
struct slider_state *sp = &sliders[sid];
void (*fun)(float); /* compiler bug */
long wid;
switch (dev) {
case REDRAW:
if (val != sliders[sid].wid) {
fprintf(stderr, "slider: Unrecognized REDRAW (val = %d)\n", val);
break;
}
wid = winget();
winset(sp->wid);
reshapeviewport();
getorigin(&sp->orig[X], &sp->orig[Y]);
draw_slider(sid);
if (wid != -1)
winset(wid);
break;
case WINQUIT:
if (val != sliders[sid].wid) {
fprintf(stderr, "slider: Unrecognized WINQUIT (val = %d)\n", val);
break;
}
close_slider(sid);
return(0);
case ESCKEY:
if (val)
close_slider(sid);
return(0);
case LEFTMOUSE:
sp->state = val? SLIDER_SLIDING: SLIDER_IDLE;
sp->omx = getvaluator(MOUSEX);
if (val) {
sp->val = sp->lo +
((float)(sp->omx - sp->orig[X]) - sp->ctl_points[0][X])
* sp->unit;
if (sp->val < sp->lo)
sp->val = sp->lo;
if (sp->val > sp->hi)
sp->val = sp->hi;
fun = sp->fun;
(*fun)(sp->val);
}
break;
case MIDDLEMOUSE:
sp->state = val? SLIDER_SLIDING: SLIDER_IDLE;
sp->omx = getvaluator(MOUSEX);
break;
case RIGHTMOUSE:
if (val) return(do_slider_menu(sid));
break;
default:
fprintf(stderr, "slider: Unrecognized event (dev = %d, val = %d)\n",
dev, val);
break;
}
return(1);
}
void
do_slider(int sid)
{
struct slider_state *sp = &sliders[sid];
void (*fun)(float); /* compiler bug */
short nmx;
if (sp->state == SLIDER_IDLE)
return;
nmx = getvaluator(MOUSEX);
sp->val += (float)(nmx - sp->omx) * sp->unit;
sp->omx = nmx;
if (sp->val < sp->lo)
sp->val = sp->lo;
else
if (sp->val > sp->hi)
sp->val = sp->hi;
fun = sp->fun;
(*fun)(sp->val);
draw_slider(sid);
}
int
do_slider_menu(int sid)
{
struct slider_state *sp = &sliders[sid];
void (*fun)(float); /* compiler bug */
char menu_buf[100];
int val;
if (sp->menu == NULL) {
sprintf(menu_buf, "%s %%t|reset (to %g)|quit", sp->title, sp->dflt);
sp->menu = defpup(menu_buf);
}
val = dopup(sp->menu);
switch (val) {
case -1:
break;
case 1:
sp->val = sp->dflt;
fun = sp->fun;
(*fun)(sp->val);
draw_slider(sid);
break;
case 2:
close_slider(sid);
return(0);
default:
fprintf(stderr, "slider: Unrecognized menu selection (%d)\n", val);
break;
}
return(1);
}